package com.cai.servlet.user;

import com.cai.dao.UserDao;
import com.cai.dao.impl.UserDaoImpl;
import com.cai.entity.User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author RuoLi
 * 修改密码
 * 未完成：更改新密码没有确认旧密码
 */
@WebServlet(name = "ServletUpdatePassword", value = "/updatePassword")
public class ServletUpdatePassword extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = new User();
        HttpSession session = request.getSession();
        //获取账号密码
        String username = (String) session.getAttribute("user_name");
        String password = (String) session.getAttribute("password");
        user.setUser(username);
        user.setPassword(password);
        UserDao userDao = new UserDaoImpl();
        user = userDao.userInfoLogin(user);
        session.setAttribute("email", user.getUser_email());
        request.getRequestDispatcher("revise_password.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收数据
        HttpSession session = request.getSession();
        String newPassword = request.getParameter("password");
        User user = new User();
        user.setUser((String) session.getAttribute("user_name"));
        user.setPassword((String) session.getAttribute("password"));
        UserDao userDao = new UserDaoImpl();
        int num = userDao.userUpdatePassword(newPassword, user);
        if (num!=0){
            //修改成功
            response.getWriter().println(1);
        }else {
            //修改失败
            response.getWriter().println(0);
        }
    }
}
